/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
/*
 * Created on 06.02.2005
 *
 */
package soccerscope.daistatistic;

import java.util.Vector;

import robocup.component.RobocupAgent;
import robocup.component.StateTactic;
import soccerscope.SoccerScope;
import soccerscope.SoccerScopeMenuBar;
import soccerscope.net.AgentConnector;

/**
 * 
 * class to register the playerbeans, the tactics and the coach so they can be
 * used by the gui
 */
public class Register {

	static int registeredPlayers = 0;

	static Vector<RobocupAgent> playerBeans = new Vector<RobocupAgent>();

	static Vector<StateTactic> tactics = new Vector<StateTactic>();

	public static SoccerScopeMenuBar scopeMenu;

	public static SoccerScope soccerScope = null;

	public static boolean saveDaiWorldModel = true;

	/**
	 * 
	 * register a PlayerBean
	 * 
	 * @param PlayerBean
	 *            bean
	 */
	public static void registerPlayerBean(RobocupAgent bean) {

		registeredPlayers++;
		playerBeans.add(bean);

	}

	/**
	 * 
	 * register a TacticBean
	 * 
	 * @param TacticBean
	 *            bean
	 */
	public static void registerTactic(StateTactic tactic) {
		tactics.add(tactic);
	}

	/**
	 * get the playerbean of a specified player
	 * 
	 * @param playerNumber
	 *            the playernumber
	 */
	public static RobocupAgent getPlayerBeanWithPlayerNumber(int playerNumber) {
		for (RobocupAgent pBean : playerBeans) {
			if (pBean.getWorld().getNumber() == playerNumber) {
				if (pBean.getTeamName() != null) {
					if (pBean.getTeamName().equals(
							playerBeans.firstElement().getTeamName())) // only
																		// players
																		// from
																		// the
																		// first
																		// registered
																		// team
						return pBean;
				}
			}
		}
		return null;
	}

	/**
	 * 
	 * @return
	 */
	public synchronized static int getNumberOfPlayersLeftTeam() {
		int num = 0;

		synchronized (playerBeans) {
			for (RobocupAgent pBean : playerBeans) {
				if (pBean != null && playerBeans.firstElement() != null)
					if (pBean.getTeamName() != null
							&& playerBeans.firstElement().getTeamName() != null)
						if (pBean.getTeamName().equals(
								playerBeans.firstElement().getTeamName())) {
							num++;

						} else {
							if (pBean.getTeamName() == null && num < 11) {
								num++;
							}
						}
			}
		}
		return num;
	}

	/**
	 * get the tactic of an specified player
	 * 
	 * @param int
	 *            playerNumber the playernumber
	 */
	public static StateTactic getTacticOfPlayer(int playerNumber) {
		for (StateTactic tBean : tactics) {
			if (tBean.getAgent().getWorld().getNumber() == playerNumber) {
				return tBean;
			}
		}
		return null;
	}

	/**
	 * updates the dai history of a player
	 * 
	 * @param pBean
	 */
	public static void infoReceived(RobocupAgent pBean) {
		if (saveDaiWorldModel)
			if (pBean != null && pBean.getNumber() > 0
					&& pBean.getWorld() != null
					&& pBean.getWorld().getLongTermMemory() != null)
				if (pBean.getTeamName().equals(
						playerBeans.firstElement().getTeamName()))
					if (pBean.getWorld().getBallRef() != null)
						if (pBean.getWorld().getBallRef().getPosition() != null)
							AgentConnector
									.addData(pBean, scopeMenu);// getPlayerBeanWithPlayerNumber(pBean.getMyWorld().getNumber()));

	}

  /**
	 * updates player pass
	 * 
	 * @param shooter
	 * @param reciever
	 */
	public static void passToTeammate(RobocupAgent shooter, int reciever) {
		if (shooter.getTeamName().equals(
				playerBeans.firstElement().getTeamName())
				&& saveDaiWorldModel)
			AgentConnector.addPassData(shooter, reciever);
	}

	/**
	 * updates player holes
	 * 
	 * @param playerNumber
	 * @param numberOfNewHoles
	 */
	public static void hole(RobocupAgent shooter, int numberOfNewHoles) {

		AgentConnector.addHoles(shooter, numberOfNewHoles);
		/*
		 * if (shooter == null) return; if
		 * (shooter.getTeamName().equals(playerBeans.firstElement().getTeamName()) &&
		 * saveDaiWorldModel)
		 * DaiWorldModelHistory.addHoles(shooter,numberOfNewHoles);
		 */

	}

//	/**
//	 * add the performed state to history
//	 * 
//	 * @param state,
//	 *            the performed state
//	 * @param wm,
//	 *            the player worldmodel
//	 */
//	public static void addState(String state, WorldModel wm) {
//		DaiWorldModelHistory.addState(wm.getCycle(), wm.getNumber(), state);
//	}

	/**
	 * add state assessements to history
	 * 
	 * @param stateType,
	 *            the state for the assessment
	 * @param ass,
	 *            the assessment
	 * @param wm,
	 *            the player worldmodel
	 */
	public static void addStateAss(String stateInfo, RobocupAgent agent) {
		AgentConnector.addStateAss(agent,
				stateInfo);
	}
}

